package org.htmlparser.lexer;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;

public class InputStreamSource extends Source
{
  public static int BUFFER_SIZE = 16384;
  protected char[] mBuffer;
  protected String mEncoding;
  protected int mLevel;
  protected int mMark;
  protected int mOffset;
  protected transient InputStreamReader mReader;
  protected transient InputStream mStream;

  public InputStreamSource(InputStream paramInputStream)
    throws UnsupportedEncodingException
  {
    this(paramInputStream, null, BUFFER_SIZE);
  }

  public InputStreamSource(InputStream paramInputStream, String paramString)
    throws UnsupportedEncodingException
  {
    this(paramInputStream, paramString, BUFFER_SIZE);
  }

  public InputStreamSource(InputStream paramInputStream, String paramString, int paramInt)
    throws UnsupportedEncodingException
  {
    if (paramInputStream == null)
    {
      paramInputStream = new Stream(null);
      this.mStream = paramInputStream;
      if (paramString != null)
        break label92;
      this.mReader = new InputStreamReader(paramInputStream);
      this.mEncoding = this.mReader.getEncoding();
    }
    while (true)
    {
      while (true)
      {
        do
        {
          this.mBuffer = new char[paramInt];
          this.mLevel = 0;
          this.mOffset = 0;
          this.mMark = -1;
          return;
        }
        while (paramInputStream.markSupported());
        paramInputStream = new Stream(paramInputStream);
      }
      label92: this.mEncoding = paramString;
      this.mReader = new InputStreamReader(paramInputStream, paramString);
    }
  }

  private void readObject(ObjectInputStream paramObjectInputStream)
    throws IOException, ClassNotFoundException
  {
    paramObjectInputStream.defaultReadObject();
    if (this.mBuffer != null)
      this.mStream = new ByteArrayInputStream(new byte[0]);
  }

  private void writeObject(ObjectOutputStream paramObjectOutputStream)
    throws IOException
  {
    if (this.mStream != null)
    {
      int i = this.mOffset;
      char[] arrayOfChar = new char[4096];
      do;
      while (-1 != read(arrayOfChar));
      this.mOffset = i;
    }
    paramObjectOutputStream.defaultWriteObject();
  }

  public int available()
  {
    int i;
    if (this.mStream == null)
      i = 0;
    while (true)
    {
      return i;
      i = this.mLevel - this.mOffset;
    }
  }

  public void close()
    throws IOException
  {
  }

  public void destroy()
    throws IOException
  {
    this.mStream = null;
    if (this.mReader != null)
      this.mReader.close();
    this.mReader = null;
    this.mBuffer = null;
    this.mLevel = 0;
    this.mOffset = 0;
    this.mMark = -1;
  }

  protected void fill(int paramInt)
    throws IOException
  {
    int i;
    char[] arrayOfChar;
    int j;
    int k;
    if (this.mReader != null)
    {
      i = this.mBuffer.length - this.mLevel;
      if (i >= paramInt)
        break label102;
      k = 2 * this.mBuffer.length;
      int l = paramInt + this.mLevel;
      if (k >= l)
        break label91;
      k = l;
      arrayOfChar = new char[k];
      j = this.mReader.read(arrayOfChar, this.mLevel, paramInt);
      if (-1 != j)
        break label112;
      this.mReader.close();
      this.mReader = null;
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          return;
          label91: paramInt = k - this.mLevel;
        }
        label102: arrayOfChar = this.mBuffer;
        paramInt = i;
      }
      if (this.mBuffer != arrayOfChar)
      {
        label112: System.arraycopy(this.mBuffer, 0, arrayOfChar, 0, this.mLevel);
        this.mBuffer = arrayOfChar;
      }
      this.mLevel = (j + this.mLevel);
    }
  }

  public char getCharacter(int paramInt)
    throws IOException
  {
    if (this.mStream == null)
      throw new IOException("source is closed");
    if (paramInt >= this.mBuffer.length)
      throw new IOException("illegal read ahead");
    return this.mBuffer[paramInt];
  }

  public void getCharacters(StringBuffer paramStringBuffer, int paramInt1, int paramInt2)
    throws IOException
  {
    if (this.mStream == null)
      throw new IOException("source is closed");
    paramStringBuffer.append(this.mBuffer, paramInt1, paramInt2);
  }

  public void getCharacters(char[] paramArrayOfChar, int paramInt1, int paramInt2, int paramInt3)
    throws IOException
  {
    if (this.mStream == null)
      throw new IOException("source is closed");
    System.arraycopy(this.mBuffer, paramInt2, paramArrayOfChar, paramInt1, paramInt3 - paramInt2);
  }

  public String getEncoding()
  {
    return this.mEncoding;
  }

  public InputStream getStream()
  {
    return this.mStream;
  }

  public String getString(int paramInt1, int paramInt2)
    throws IOException
  {
    if (this.mStream == null)
      throw new IOException("source is closed");
    if (paramInt1 + paramInt2 > this.mBuffer.length)
      throw new IOException("illegal read ahead");
    return new String(this.mBuffer, paramInt1, paramInt2);
  }

  public void mark(int paramInt)
    throws IOException
  {
    if (this.mStream == null)
      throw new IOException("source is closed");
    this.mMark = this.mOffset;
  }

  public boolean markSupported()
  {
    return true;
  }

  public int offset()
  {
    int i;
    if (this.mStream == null)
      i = -1;
    while (true)
    {
      return i;
      i = this.mOffset;
    }
  }

  public int read()
    throws IOException
  {
    int j;
    if (this.mLevel - this.mOffset < 1)
    {
      if (this.mStream == null)
        throw new IOException("source is closed");
      fill(1);
      if (this.mOffset >= this.mLevel)
        j = -1;
    }
    while (true)
    {
      while (true)
      {
        return j;
        char[] arrayOfChar2 = this.mBuffer;
        int k = this.mOffset;
        this.mOffset = (k + 1);
        j = arrayOfChar2[k];
      }
      char[] arrayOfChar1 = this.mBuffer;
      int i = this.mOffset;
      this.mOffset = (i + 1);
      j = arrayOfChar1[i];
    }
  }

  public int read(char[] paramArrayOfChar)
    throws IOException
  {
    return read(paramArrayOfChar, 0, paramArrayOfChar.length);
  }

  public int read(char[] paramArrayOfChar, int paramInt1, int paramInt2)
    throws IOException
  {
    int i;
    if (this.mStream == null)
      throw new IOException("source is closed");
    if ((paramArrayOfChar == null) || (paramInt1 < 0) || (paramInt2 < 0))
    {
      String str;
      StringBuffer localStringBuffer = new StringBuffer().append("illegal argument read (");
      if (paramArrayOfChar == null)
        str = "null";
      while (true)
      {
        throw new IOException(str + ", " + paramInt1 + ", " + paramInt2 + ")");
        str = "cbuf";
      }
    }
    if (this.mLevel - this.mOffset < paramInt2)
      fill(paramInt2 - this.mLevel - this.mOffset);
    if (this.mOffset >= this.mLevel)
      i = -1;
    while (true)
    {
      return i;
      i = Math.min(this.mLevel - this.mOffset, paramInt2);
      System.arraycopy(this.mBuffer, this.mOffset, paramArrayOfChar, paramInt1, i);
      this.mOffset = (i + this.mOffset);
    }
  }

  public boolean ready()
    throws IOException
  {
    int i;
    if (this.mStream == null)
      throw new IOException("source is closed");
    if (this.mOffset < this.mLevel)
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  public void reset()
    throws IllegalStateException
  {
    if (this.mStream == null)
      throw new IllegalStateException("source is closed");
    if (-1 != this.mMark)
      this.mOffset = this.mMark;
    while (true)
    {
      return;
      this.mOffset = 0;
    }
  }

  // ERROR //
  public void setEncoding(String paramString)
    throws org.htmlparser.util.ParserException
  {
    // Byte code:
    //   0: aload_0
    //   1: invokevirtual 175	org/htmlparser/lexer/InputStreamSource:getEncoding	()Ljava/lang/String;
    //   4: astore_2
    //   5: aload_2
    //   6: aload_1
    //   7: invokevirtual 179	java/lang/String:equalsIgnoreCase	(Ljava/lang/String;)Z
    //   10: ifne +295 -> 305
    //   13: aload_0
    //   14: invokevirtual 181	org/htmlparser/lexer/InputStreamSource:getStream	()Ljava/io/InputStream;
    //   17: astore_3
    //   18: aload_0
    //   19: getfield 50	org/htmlparser/lexer/InputStreamSource:mBuffer	[C
    //   22: astore 5
    //   24: aload_0
    //   25: getfield 54	org/htmlparser/lexer/InputStreamSource:mOffset	I
    //   28: istore 6
    //   30: aload_3
    //   31: invokevirtual 183	java/io/InputStream:reset	()V
    //   34: aload_0
    //   35: aload_1
    //   36: putfield 48	org/htmlparser/lexer/InputStreamSource:mEncoding	Ljava/lang/String;
    //   39: aload_0
    //   40: new 39	java/io/InputStreamReader
    //   43: dup
    //   44: aload_3
    //   45: aload_1
    //   46: invokespecial 64	java/io/InputStreamReader:<init>	(Ljava/io/InputStream;Ljava/lang/String;)V
    //   49: putfield 42	org/htmlparser/lexer/InputStreamSource:mReader	Ljava/io/InputStreamReader;
    //   52: aload_0
    //   53: aload_0
    //   54: getfield 50	org/htmlparser/lexer/InputStreamSource:mBuffer	[C
    //   57: arraylength
    //   58: newarray char
    //   60: putfield 50	org/htmlparser/lexer/InputStreamSource:mBuffer	[C
    //   63: aload_0
    //   64: iconst_0
    //   65: putfield 52	org/htmlparser/lexer/InputStreamSource:mLevel	I
    //   68: aload_0
    //   69: iconst_0
    //   70: putfield 54	org/htmlparser/lexer/InputStreamSource:mOffset	I
    //   73: aload_0
    //   74: bipush 255
    //   76: putfield 56	org/htmlparser/lexer/InputStreamSource:mMark	I
    //   79: iload 6
    //   81: ifeq +224 -> 305
    //   84: iload 6
    //   86: newarray char
    //   88: astore 8
    //   90: iload 6
    //   92: aload_0
    //   93: aload 8
    //   95: invokevirtual 86	org/htmlparser/lexer/InputStreamSource:read	([C)I
    //   98: if_icmpeq +70 -> 168
    //   101: new 174	org.htmlparser.util.ParserException
    //   104: dup
    //   105: ldc 185
    //   107: invokespecial 186	org.htmlparser.util.ParserException:<init>	(Ljava/lang/String;)V
    //   110: athrow
    //   111: astore 7
    //   113: new 174	org.htmlparser.util.ParserException
    //   116: dup
    //   117: aload 7
    //   119: invokevirtual 191	java/lang/Throwable:getMessage	()Ljava/lang/String;
    //   122: aload 7
    //   124: invokespecial 194	org.htmlparser.util.ParserException:<init>	(Ljava/lang/String;Ljava/lang/Throwable;)V
    //   127: athrow
    //   128: astore 4
    //   130: new 174	org.htmlparser.util.ParserException
    //   133: dup
    //   134: new 121	java/lang/StringBuffer
    //   137: dup
    //   138: invokespecial 141	java/lang/StringBuffer:<init>	()V
    //   141: ldc 196
    //   143: invokevirtual 146	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   146: aload 4
    //   148: invokevirtual 191	java/lang/Throwable:getMessage	()Ljava/lang/String;
    //   151: invokevirtual 146	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   154: ldc 198
    //   156: invokevirtual 146	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   159: invokevirtual 158	java/lang/StringBuffer:toString	()Ljava/lang/String;
    //   162: aload 4
    //   164: invokespecial 194	org.htmlparser.util.ParserException:<init>	(Ljava/lang/String;Ljava/lang/Throwable;)V
    //   167: athrow
    //   168: iconst_0
    //   169: istore 9
    //   171: iload 9
    //   173: iload 6
    //   175: if_icmpge +130 -> 305
    //   178: aload 8
    //   180: iload 9
    //   182: caload
    //   183: aload 5
    //   185: iload 9
    //   187: caload
    //   188: if_icmpeq +111 -> 299
    //   191: new 200	org/htmlparser/util/EncodingChangeException
    //   194: dup
    //   195: new 121	java/lang/StringBuffer
    //   198: dup
    //   199: invokespecial 141	java/lang/StringBuffer:<init>	()V
    //   202: ldc 202
    //   204: invokevirtual 146	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   207: aload 8
    //   209: iload 9
    //   211: caload
    //   212: invokevirtual 205	java/lang/StringBuffer:append	(C)Ljava/lang/StringBuffer;
    //   215: ldc 207
    //   217: invokevirtual 146	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   220: aload 8
    //   222: iload 9
    //   224: caload
    //   225: bipush 16
    //   227: invokestatic 211	java/lang/Integer:toString	(II)Ljava/lang/String;
    //   230: invokevirtual 146	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   233: ldc 213
    //   235: invokevirtual 146	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   238: ldc 207
    //   240: invokevirtual 146	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   243: aload 5
    //   245: iload 9
    //   247: caload
    //   248: bipush 16
    //   250: invokestatic 211	java/lang/Integer:toString	(II)Ljava/lang/String;
    //   253: invokevirtual 146	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   256: aload 5
    //   258: iload 9
    //   260: caload
    //   261: invokevirtual 205	java/lang/StringBuffer:append	(C)Ljava/lang/StringBuffer;
    //   264: ldc 215
    //   266: invokevirtual 146	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   269: aload_2
    //   270: invokevirtual 146	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   273: ldc 217
    //   275: invokevirtual 146	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   278: aload_1
    //   279: invokevirtual 146	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   282: ldc 219
    //   284: invokevirtual 146	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   287: iload 9
    //   289: invokevirtual 153	java/lang/StringBuffer:append	(I)Ljava/lang/StringBuffer;
    //   292: invokevirtual 158	java/lang/StringBuffer:toString	()Ljava/lang/String;
    //   295: invokespecial 220	org/htmlparser/util/EncodingChangeException:<init>	(Ljava/lang/String;)V
    //   298: athrow
    //   299: iinc 9 1
    //   302: goto -131 -> 171
    //   305: return
    //
    // Exception table:
    //   from	to	target	type
    //   34	111	111	IOException
    //   178	299	111	IOException
    //   18	34	128	IOException
    //   113	128	128	IOException
  }

  public long skip(long paramLong)
    throws IOException, IllegalArgumentException
  {
    long l;
    if (this.mStream == null)
      throw new IOException("source is closed");
    if (0L > paramLong)
      throw new IllegalArgumentException("cannot skip backwards");
    if (this.mLevel - this.mOffset < paramLong)
      fill((int)(paramLong - this.mLevel - this.mOffset));
    if (this.mOffset >= this.mLevel)
      l = -1L;
    while (true)
    {
      return l;
      l = Math.min(this.mLevel - this.mOffset, paramLong);
      this.mOffset = (int)(l + this.mOffset);
    }
  }

  public void unread()
    throws IOException
  {
    if (this.mStream == null)
      throw new IOException("source is closed");
    if (this.mOffset > 0)
    {
      this.mOffset -= 1;
      return;
    }
    throw new IOException("can't unread no characters");
  }
}